package com.cne.gmail.Gmail.Util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


public class OSSUtils {
    static final String DEFAULT_OSS_END_POINT = "http://mailcnexpscom.oss-cn-hangzhou.aliyuncs.com";
    static final String DEFAULT_OSS_BUCKET = "filescnexpscom";
    static final String DEFAULT_ID = "KdJEdE7dSJfzdOD0";
    static final String DEFAULT_KEY = "pWzJ6EXhNQ5MMMVR7QffShfbX9dNUX";
    final String endPoint;
    String bucket = "filescnexpscom";
    final String id;
    final String key;
    ClientConfiguration conf;
    static com.commonUtil.OSSUtil defaultObject = null;

    public static com.commonUtil.OSSUtil getDefaultOSS() {
        if (null == defaultObject) {
            defaultObject = new com.commonUtil.OSSUtil("http://oss-cn-hangzhou.aliyuncs.com", "filescnegmail", DEFAULT_ID, DEFAULT_KEY, (ClientConfiguration)null);
        }

        return defaultObject;
    }

    public OSSUtils(String endPoint, String bucket, String id, String key, ClientConfiguration conf) {
        this.endPoint = endPoint;
        this.id = id;
        this.key = key;
        this.conf = conf;
    }

    protected OSSClient getOSSClient() {
        OSSClient ossClient = null;
        if (this.conf != null) {
            ossClient = new OSSClient(this.endPoint, this.id, this.key, this.conf);
        } else {
            ossClient = new OSSClient(this.endPoint, this.id, this.key);
        }

        return ossClient;
    }

    public String upLoadFileToOss(byte[] fileByte, String fileName) {
        OSSClient ossClient = this.getOSSClient();
        ByteArrayInputStream bis = new ByteArrayInputStream(fileByte);
        ObjectMetadata meta = new ObjectMetadata();
        meta.addUserMetadata("name", fileName);
        meta.setContentLength((long)fileByte.length);
        ossClient.putObject(this.bucket, fileName, bis, meta);
        ossClient.shutdown();
        return fileName;
    }

    public byte[] downloadFileFromOSS(String key) throws IOException {
        OSSClient ossClient = this.getOSSClient();

        try {
            OSSObject object = ossClient.getObject(this.bucket, key);
            byte[] result = null;
            InputStream is = object.getObjectContent();
            if (is != null) {
                result = input2byte(is);
            }

            ossClient.shutdown();
            return result;
        } catch (OSSException var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static final byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        boolean var3 = false;

        int rc;
        while((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }

        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
}
